package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.GoodsItemDao;
import com.ktjy.ktmall.entity.GoodsItem;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @Author 郴州科泰
 * @Create 2024-07-19 - 16:25
 */
public class GoodsItemDaoImpl extends BaseDao implements GoodsItemDao {

    /**
     * 分页查询商品列表信息
     * @param from 起始位置偏移量
     * @param pageSize 每页显示的数据行数
     * @return 返回查询到的分页列表数据
     */
    @Override
    public List<GoodsItem> getGoodsListPage(Integer from, Integer pageSize) {
        List<GoodsItem> list = new ArrayList<>();
        // 查询已上架的商品分页列表
        String sql = "select * from `t_goods_info` where `publish_status` = 1 limit ?,?";
        rs = super.executeQuery(sql, from, pageSize);
        try {
            while (rs.next()) {
                GoodsItem g = new GoodsItem(rs.getInt("id"), rs.getString("goods_name"), rs.getString("spu_code"), rs.getInt("category_id"), rs.getInt("brand_id"), rs.getString("goods_img"), rs.getString("goods_desc"),rs.getBigDecimal("goods_price"), rs.getInt("publish_status"), rs.getInt("order_num"),rs.getString("mainPictures"),rs.getString("detailsPictures"),rs.getString("create_time"),rs.getString("update_time"));
                list.add(g);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    // 得到总的商品列表记录数
    @Override
    public int getCount() {
        int count = 0;
        String sql = "select count(*) from `t_goods_info` where `publish_status` = 1";
        rs = super.executeQuery(sql);
        try {
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return count;
    }
    /**
     * 通过二级商品分类id查询此商品分类下的商品列表
     * @param categoryId 二级商品分类id
     */
    @Override
    public List<GoodsItem> getGoodsByCategoryId(Integer categoryId) {
        List<GoodsItem> list = new ArrayList<>();
        // 查询已上架的商品分页列表
        String sql = "select * from `t_goods_info` where `publish_status` = 1 AND category_id = ?";
        rs = super.executeQuery(sql, categoryId);
        try {
            while (rs.next()) {
                GoodsItem g = new GoodsItem(rs.getInt("id"), rs.getString("goods_name"), rs.getString("spu_code"), rs.getInt("category_id"), rs.getInt("brand_id"), rs.getString("goods_img"), rs.getString("goods_desc"),rs.getBigDecimal("goods_price"), rs.getInt("publish_status"), rs.getInt("order_num"),rs.getString("mainPictures"),rs.getString("detailsPictures"),rs.getString("create_time"),rs.getString("update_time"));
                list.add(g);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    /**
     * 通过商品id查询商品的详情信息
     * @param id 商品id
     * @return 返回查询到的商品详情信息
     */
    @Override
    public GoodsItem findById(int id) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        GoodsItem g = null;
        String sql = "select * from `t_goods_info` where id = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, id);
            rs = pstmt.executeQuery();
            if (!rs.isClosed() && rs.next()) {
                g = new GoodsItem(rs.getInt("id"), rs.getString("goods_name"), rs.getString("spu_code"), rs.getInt("category_id"), rs.getInt("brand_id"), rs.getString("goods_img"), rs.getString("goods_desc"),rs.getBigDecimal("goods_price"), rs.getInt("publish_status"), rs.getInt("order_num"),rs.getString("mainPictures"),rs.getString("detailsPictures"),rs.getString("create_time"),rs.getString("update_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return g;
    }
}
